package com.adds.lvds.security.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.adds.lvds.core.plugin.PageView;
import com.adds.lvds.core.utils.CommUtils;
import com.adds.lvds.security.dao.SysResourcesMapper;
import com.adds.lvds.security.model.SysResource;
import com.adds.lvds.security.model.SysResourceRole;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Transactional
@Service("sysResourceService")
public class SysResourceService {
	@Autowired
	private SysResourcesMapper resourcesMapper;

	/**
	 * 分页显示资源表数据
	 * 
	 * @param pageView
	 * @param resources
	 * @return
	 */
	public PageView<SysResource> query(PageView<SysResource> pageView, SysResource resources) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			map.put("paging", pageView);
			map.put("t", resources);
			List<SysResource> list = resourcesMapper.pageSysResourcesByCondition(map);
			pageView.setRecords(list);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return pageView;
	}

	/**
	 * 录入资源表数据
	 * 
	 * @param t
	 * @return
	 */
	public String addSysResources(SysResource t) {
		String result = CommUtils.SUCC;
		try {
			this.resourcesMapper.addSysResources(t);
		} catch (Exception e) {
			e.printStackTrace();
			result = CommUtils.ERROR;
		}
		return result;

	}

	/**
	 * 编辑资源表数据
	 * 
	 * @param t
	 * @return
	 */
	public String updateSysResources(SysResource t) {
		String result = CommUtils.SUCC;
		try {
			this.resourcesMapper.updateSysResources(t);
		} catch (Exception e) {
			e.printStackTrace();
			result = CommUtils.ERROR;
		}
		return result;

	}

	/**
	 * 删除资源表数据
	 * 
	 * @param id
	 * @return
	 */
	public String deleteSysResource(String id) {
		String result = CommUtils.SUCC;
		try {
			this.resourcesMapper.batchSysResources(id.split(","));
		} catch (Exception e) {
			e.printStackTrace();
			result = CommUtils.ERROR;
		}
		return result;

	}

	/**
	 * 根据条件获取全部资源
	 * 
	 * @param t
	 * @return
	 */
	public List<SysResource> queryAll(SysResource t) {
		List<SysResource> list = null;
		try {
			list = resourcesMapper.queryAll(t);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	public SysResource getSysResourcesByMap(Map<String, Object> map) {
		SysResource sysResource = null;
		try {
			sysResource = resourcesMapper.getSysResourcesByMap(map);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return sysResource;
	}

	/**
	 * 根据用户id获取对应的资源数据
	 * 
	 * @param userId
	 * @return
	 */
	public List<SysResource> findUserResources(Integer userId) {
		List<SysResource> list = null;
		try {
			list = resourcesMapper.findUserResources(userId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	/**
	 * 根据角色id，获取对应的资源数据
	 * 
	 * @param roleId
	 * @return
	 */
	public List<SysResource> findRoleRes(String roleId) {
		List<SysResource> list = null;
		try {
			list = resourcesMapper.findRoleRes(roleId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	/**
	 * 根据角色id，获取对应的资源数据
	 * 
	 * @param roleId
	 * @return
	 */
	public List<SysResource> findSysResourceByMap(Map<String, Object> map) {
		List<SysResource> list = null;
		try {
			list = resourcesMapper.findSysResourceByMap(map);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}
	/**
	 * 根据角色id，获取对应的资源数据
	 * 
	 * @param roleId
	 * @return
	 */
	public List<SysResource> querySysResourceByMap(Map<String, Object> map) {
		List<SysResource> list = null;
		try {
			list = resourcesMapper.querySysResourceByMap(map);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	/**
	 * 录入角色资源数据
	 * 
	 * @param roleId
	 * @param resIds
	 */
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public String addRoleRes(String roleId, String resIds) {
		String result = CommUtils.SUCC;
		try {
			resourcesMapper.deleteResourcesRole(roleId);
			List<SysResourceRole> list = new ArrayList<SysResourceRole>();
			for (String string : resIds.split(",")) {
				SysResourceRole t = new SysResourceRole();
				t.setRoleId(roleId);
				t.setResId(string);
				list.add(t);
			}
			resourcesMapper.addBatchRoleRes(list);
		} catch (Exception e) {
			result = CommUtils.ERROR;
			throw new RuntimeException();
		}
		return result;
	}

	public List<Object> getMenuJson(String resourceid, String parentid) {
		Map<String, Object> map = new HashMap<>();
		List<Object> treeList = new ArrayList<Object>();
		List<Object> childList = null;
		if (StringUtils.isEmpty(resourceid)) {
			map.put("role_id", 1);
			map.put("type", 1);
		} else {
			map.put("role_id", 1);
			map.put("parentid", resourceid);
		}
		try {
			List<SysResource> rs = resourcesMapper.findSysResourceByMap(map);
			for (SysResource sysResource : rs) {
				childList = new ArrayList<Object>();
				JSONObject json_parent = new JSONObject();
				json_parent.put("id", sysResource.getId());
				json_parent.put("name", sysResource.getName());
				json_parent.put("pId", sysResource.getParentId());
				if (StringUtils.isNotEmpty(parentid)) {
					if (sysResource.getId() == Integer.parseInt(parentid)) {
						json_parent.put("checked", true);
						json_parent.put("open", true);
						json_parent.put("show_switch", false);
					}else {
						json_parent.put("show_switch", true);
					}
				}
				if (sysResource.getChildren() != null && sysResource.getChildren().size() > 0) {
					
					json_parent.put("isParent", true);
					List<SysResource> list_1 = sysResource.getChildren();
					for (SysResource sysResource2 : list_1) {
						JSONObject objcect = new JSONObject();
						objcect.put("id", sysResource2.getId());
						objcect.put("name", sysResource2.getName());
						objcect.put("pId", sysResource2.getParentId());
						if (StringUtils.isNotEmpty(parentid)) {
							if (sysResource2.getId() == Integer.parseInt(parentid)) {
								objcect.put("checked", true);
								objcect.put("open", true);
							}
						}
						map.clear();
						map.put("role_id", 1);
						map.put("parentid", sysResource2.getId());
						List<SysResource> list_2 = resourcesMapper.findSysResourceByMap(map);
						if (!list_2.isEmpty() && list_2.size() > 0) {
							objcect.put("isParent", true);
						}
						childList.add(objcect);
					}

				} 
				JSONArray childJson = JSONArray.fromObject(childList);
				json_parent.put("children", childJson);
				treeList.add(json_parent);
			}
		} catch (Exception e) {
		}
		return treeList;
	}
}
